import axios from 'axios'
import Vue from 'vue'
import router from '../../routers/index'
const request = axios.create({
    // baseURL: 'http://localhost:3001/shopping/api',
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 600 * 1000
  })

  request.interceptors.request.use(config => {
    let token = window.localStorage.getItem('shoppingToken')
    if (token) config.headers.Authorization =  `Bearer ${token}`
    return config
  })

  request.interceptors.response.use(response => {
    return response.data
  }, err => {
    Vue.prototype.$message({
      type: 'error',
      message: err.response.data.msg
    })
    if (err.response.status === 401) {
      router.push('/login')
    }
    return Promise.reject(err)
  })

  export default request